package org.mylitespring.context.support;

import org.mylitespring.beans.factory.support.DefaultBeanFactory;
import org.mylitespring.beans.factory.xml.XmlBeanDefinitionReader;
import org.mylitespring.context.ApplicationContext;
import org.mylitespring.core.io.Resource;
import org.mylitespring.util.ClassUtils;

public abstract class AbstractApplicationContext implements ApplicationContext{

	private DefaultBeanFactory factory=null;
	private ClassLoader beanClassLoader;
	
	public AbstractApplicationContext(String configFile) {
		factory=new DefaultBeanFactory();
		XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
		Resource resource=this.getResourceByPath(configFile);
		reader.loadBeanDefinitions(resource);
		factory.setBeanClassLoader(this.getBeanClassLoader());
	}
	
	

	public Object getBean(String beanID) {
		return factory.getBean(beanID);
	}



	protected abstract Resource getResourceByPath(String configFile);
	
	public void setBeanClassLoader(ClassLoader beanClassLoader) {
		this.beanClassLoader=beanClassLoader;
	}
	
	public ClassLoader getBeanClassLoader() {
		return (this.beanClassLoader!=null?this.beanClassLoader:ClassUtils.getDefaultClassLoader());
	}
}
